package cn.wps.sdk.demo.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.wps.moffice.sdk.api.bean.FileType;
import cn.wps.sdk.demo.util.FileCopyTool;
import cn.wps.sdk.demo.util.FileUtil;
import cn.wps.sdk.demo.R;

public abstract class MultiSelectFileActivity extends Activity implements View.OnClickListener ,IAction{

    private FileAdapter mFileAdapter;
    private FileCopyTool mFileTool;
    private Button execute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_document);
        mFileTool = new FileCopyTool(this);
        ListView listView = findViewById(R.id.list);
        execute = findViewById(R.id.execute);
        execute.setText(executeName());
        mFileAdapter = new FileAdapter();
        listView.setAdapter(mFileAdapter);
        findViewById(R.id.execute).setOnClickListener(this);
        Button mSelectFileBtn = findViewById(R.id.select_file);
        mSelectFileBtn.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshDocument();
    }

    private void refreshDocument() {
        List<File> list = mFileTool.getAllFile();
        List<FileItem> fileItems = new ArrayList<>();
        for (File file : list) {
            FileItem fileItem = new FileItem();
            fileItem.filePath = file.getAbsolutePath();
            fileItem.isChecked = false;
            fileItems.add(fileItem);
        }
        mFileAdapter.updateData(fileItems);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.select_file) {
            selectFile();
        } else if (view.getId() == R.id.execute) {
            List<FileItem> fileSelectList = mFileAdapter.getData();
            List<FileItem> result = new ArrayList<>();
            for (FileItem fileSelect : fileSelectList) {
                if (fileSelect.isChecked) {
                    result.add(fileSelect);
                }
            }
            execute(result);

        }
    }

    private void selectFile() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        startActivityForResult(intent, 1);
    }


    public static class FileAdapter extends BaseAdapter {

        private List<FileItem> mPath;

        public void updateData(List<FileItem> data) {
            if (mPath == null) {
                mPath = new ArrayList<>();
            } else {
                mPath.clear();
            }
            mPath.addAll(data);
            notifyDataSetChanged();
        }

        public List<FileItem> getData() {
            return mPath;
        }


        @Override
        public int getCount() {
            return mPath == null ? 0: mPath.size();
        }

        @Override
        public Object getItem(int position) {
            return mPath == null ? null : mPath.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.document_item, parent, false);
            TextView textView = view.findViewById(R.id.text);
            textView.setText(FileUtil.getFileName(mPath.get(position).filePath));
            CheckBox checkBox = view.findViewById(R.id.check);
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mPath.get(position).isChecked = isChecked;
                }
            });
            return view;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            List<Uri> pathList = new ArrayList<>();
            if (data.getData() != null) {
                try {
                    Uri uri = data.getData();
                    pathList.add(uri);
                } catch (Throwable ignore) {
                }
            } else {
                ClipData clipData = data.getClipData();
                if (clipData != null) {
                    for (int i = 0; i < clipData.getItemCount(); i++) {
                        ClipData.Item item = clipData.getItemAt(i);
                        Uri uri = item.getUri();
                        pathList.add(uri);
                    }
                }
            }
            mFileTool.copyFromManager(pathList);
            Toast.makeText(this, "copy success", Toast.LENGTH_SHORT).show();
            refreshDocument();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


}